package com.naihe.anderson.sms.manager;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.dao.NoteDao;
import com.naihe.anderson.sms.entity.Note;
import com.naihe.anderson.sms.listener.OnRecordListener;
import com.naihe.anderson.sms.util.FileUtil;

public class NoteMgr {

	NhSmsApp application;
	NoteDao noteDao;
	OnRecordListener listener;
	
	List<Note> noteList = new ArrayList<Note>();
	private boolean ifFinishToGetData = false;
	
	public NoteMgr(NhSmsApp app){
		this.application = app;
		noteDao = new NoteDao(app);
	}
	
	public void setListener(OnRecordListener listener){
		this.listener = listener;
	}
	
	public void init(){
		new InitNoteDbTask().execute();
	}
	
	public void insert(Note note){
		new InsertOrUpdateNoteDbTask(note).execute();
	}
	
	public void update(Note note){
		new InsertOrUpdateNoteDbTask(note).execute();
	}
	
	public void setNotePasswd(Note note, String pwd){
		new SetNotePasswd(note, pwd).execute();
	}
	
	public void delNote(Note note){
		new DelNoteDbTask(note).execute();
	}
	
	//=====================================================================================================
	class SetNotePasswd extends AsyncTask<String, Object, Boolean>{
		
		Note noteTemp = null;
		String pssword = null;
		public SetNotePasswd(Note note, String pwd){
			noteTemp = note;
			pssword = pwd;
		}
		
		@Override
		protected void onPreExecute() {
		}
		
		@Override
		protected Boolean doInBackground(String... params) {
			noteDao.setNotePasswd(noteTemp.id, pssword);
			return true;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			FileUtil.addLog("NoteMgr::SetNotePasswd finish", NoteMgr.class, "NoteMgr", null);
			for(Note note : noteList){
				if(note.id == noteTemp.id){
					note.cipher = noteTemp.cipher;
					break;
				}
			}
			
			noteTemp = null;
			pssword = null;
			
			if(listener != null) listener.onDataChanged();
		}
	}
	
	class DelNoteDbTask extends AsyncTask<String, Object, Boolean>{
		
		Note noteTemp = null;
		public DelNoteDbTask(Note note){
			noteTemp = note;
		}
		
		@Override
		protected void onPreExecute() {
		}
		
		@Override
		protected Boolean doInBackground(String... params) {
			if(!TextUtils.isEmpty(noteTemp.picpath)){
				File picNoteFile = new File(FileUtil.getPicNotePath() + noteTemp.picpath + ".png");
				if(picNoteFile.exists()) picNoteFile.delete();
			}
			
			noteDao.deleteNoteEntity(noteTemp);
			return true;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			FileUtil.addLog("NoteMgr::DelNoteDbTask finish", NoteMgr.class, "NoteMgr", null);
			noteList.remove(noteTemp);
			noteTemp = null;
			
			if(listener != null) listener.onDataChanged();
		}
	}

	class InitNoteDbTask extends AsyncTask<String, Object, Boolean>{
		
		List<Note> noteListsTemp = null;
		
		public InitNoteDbTask(){
			noteListsTemp = new ArrayList<Note>();
		}
		
		@Override
		protected void onPreExecute() {
		}
		
		@Override
		protected Boolean doInBackground(String... params) {
			noteListsTemp.addAll(noteDao.queryAllNoteList());
			return true;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			FileUtil.addLog("NoteMgr::InitNoteDbTask finish", NoteMgr.class, "NoteMgr", null);
			ifFinishToGetData = true;
			noteList.addAll(noteListsTemp);
			noteListsTemp.clear();
			noteListsTemp = null;
			Collections.sort(noteList);
			
			if(listener != null) listener.onDataChanged();
		}
	}
	
	class InsertOrUpdateNoteDbTask extends AsyncTask<String, Object, Boolean>{
		
		Note note = null;
		List<Note> noteListsTemp = null;
		
		public InsertOrUpdateNoteDbTask(Note note){
			this.note = note;
			noteListsTemp = new ArrayList<Note>();
		}
		
		@Override
		protected void onPreExecute() {
		}
		
		@Override
		protected Boolean doInBackground(String... params) {
			noteDao.insertOrUpdateNoteEntity(note);
			noteListsTemp.addAll(noteDao.queryAllNoteList());
			return true;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			FileUtil.addLog("NoteMgr::InsertOrUpdateNoteDbTask finish", NoteMgr.class, "NoteMgr", null);
			
			noteList.clear();
			noteList.addAll(noteListsTemp);
			noteListsTemp.clear();
			noteListsTemp = null;
			Collections.sort(noteList);
			
			if(listener != null) listener.onDataChanged();
		}
	}
	
	//======================================================================================================
	public boolean ifFinishToGetData(){
		return ifFinishToGetData;
	}
	
	public List<Note> getNoteList(){
		return noteList;
	}
}
